package com.app.service.impl.article;

import com.app.domain.po.article.ArticlePo;
import com.app.domain.vo.MenuVo;
import com.app.domain.vo.article.ArticleVo;
import com.app.domain.vo.user.UserVo;
import com.app.framework.utils.CoBeanUtils;
import com.app.framework.utils.CollectionUtils;
import com.app.framework.utils.StringsUtils;
import com.app.mapper.article.ArticleCommonMapper;
import com.app.service.MenuService;
import com.app.service.article.ArticleCommonService;
import com.app.service.user.UserCommonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章公共方法实现
 */
@Service
public class ArticleCommonServiceImpl extends ServiceImpl<ArticleCommonMapper, ArticlePo> implements ArticleCommonService {

    @Resource
    private MenuService menuService;

    @Resource
    private UserCommonService userCommonService;

    /**
     * 更新文章访问数量
     *
     * @param articleId 文章ID
     */
    @Override
    public void updateLookCount(Long articleId) {
        baseMapper.updateLookCount(articleId);
    }

    /**
     * 更新文章点赞数量
     *
     * @param articleId 文章ID
     * @param amount    数量
     */
    @Override
    public void updateLikeCount(Long articleId, Integer amount) {
        baseMapper.updateLikeCount(articleId, amount);
    }

    /**
     * 更新文章评论数量
     *
     * @param articleId 文章ID
     * @param amount    数量
     */
    @Override
    public void updateCommentCount(Long articleId, Integer amount) {
        baseMapper.updateCommentCount(articleId, amount);
    }

    /**
     * 文章批量添加菜单信息和用户信息
     *
     * @param articleVos 文章集合
     */
    public void batchSetMenuAndUser(List<ArticleVo> articleVos) {
        List<Long> menuIds = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        for (ArticleVo articleVo : articleVos) {
            menuIds.add(articleVo.getMenuId());
            userIds.add(articleVo.getUserId());
        }
        Map<Long, MenuVo> menuMap = menuService.getMenuMap(menuIds);
        Map<Long, UserVo> userMap = userCommonService.getUserMap(userIds);
        for (ArticleVo articleVo : articleVos) {
            articleVo.setMenu(menuMap.get(articleVo.getMenuId()));
            articleVo.setUser(userMap.get(articleVo.getUserId()));
        }
    }

    /**
     * 获取文章Map
     *
     * @param articleIds 文章ID集合
     */
    @Override
    public Map<Long, ArticleVo> getArticleMap(List<Long> articleIds) {
        Map<Long, ArticleVo> articleMap = new HashMap<>();
        if (CollectionUtils.isEmpty(articleIds)) {
            return articleMap;
        }
        List<Long> uniqueArticleIds = StringsUtils.deduplication(articleIds);
        List<ArticlePo> articlePos = baseMapper.selectBatchIds(uniqueArticleIds);
        List<ArticleVo> articleVos = CoBeanUtils.copyList(articlePos, ArticleVo.class);
        batchSetMenuAndUser(articleVos);
        for (ArticleVo articleVo : articleVos) {
            articleMap.put(articleVo.getId(), articleVo);
        }
        return articleMap;
    }

}